<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
		<link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
		<link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
		<link rel="stylesheet" type="text/css" href="../../css/responsive.css">
		<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<link rel="stylesheet" href="../../js/dropdown/dropdown.css">
				
		<!-- datatable -->
		<link rel="stylesheet" href="../../js/datatables/datatable.css">
		<link rel="stylesheet" href="../../js/datepicker/datepicker.css">
		<style type="text/css">
		.text{border: 1px solid #cccccc;border-radius: 4px;padding: 6px 12px;font-size: 14px;margin-right: 20px;}
		.red{color: red;}.left{text-align: left;}
		</style>
	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
				<div class="sidebar-menu nav-collapse">

					<!-- SIDEBAR MENU -->
					<ul>
						
					</ul>
					<!-- /SIDEBAR MENU -->
				</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">
				<!-- SAMPLE BOX CONFIGURATION MODAL FORM-->
				<div class="modal fade" id="box-config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title">维护供货价</h4>
							</div>
							<div class="modal-body">
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;">SKU编码：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="skuCode" readonly="readonly" maxlength="20" />
										<input type="hidden" name="skuId" />
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;">销售价：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="salesPrice" readonly="readonly" maxlength="20" />
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;">成本价：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="costPrice" readonly="readonly" maxlength="20" />
									</div>
								</div>
								<div class="form-group">
									<label class="control-label col-md-3" style="line-height:34px;">供货价：</label>
									<div class="col-md-8">
										<input type="text" class="form-control" name="supplyPrice"  maxlength="20" value="0.00" />
										<input type="hidden" name="skuSupplyPriceId" />
									</div>
								</div>

							</div>
							<div class="modal-footer">
								<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
								<button type="button" class="btn btn-success bt-save">保存</button>
							</div>
						</div>
					</div>
				</div>
				<!-- SAMPLE BOX CONFIGURATION MODAL FORM-->
				<!-- /SAMPLE BOX CONFIGURATION MODAL FORM-->
				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row" style="height: 30px;">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="../index.html">首页</a>
											</li>
											<li>
												<a href="#">合同管理</a>
											</li>
											<li>供应商管理</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- FORMS QUERY-->
							<div class="row">
								<div class="col-md-12">
									<div class="row">
										<div class="col-md-12">
											<div class="box border primary">
												<div class="box-title">
													<h4><i class="fa fa-bars"></i>查询条件</h4>
												</div>
												<div class="box-body big" style="padding:10px;">
													<form class="form-horizontal" role="form">
														<div class="form-group mrg-bt10">
															<label class="col-sm-1 control-label" style="width: auto">商品编码：</label>
															<div class="col-sm-2">
																<input type="text" class="form-control" name="productCode" placeholder="输入关键字">
															</div>
															<label class="col-sm-1 control-label" style="width: auto">商品名称：</label>
															<div class="col-sm-2">
																<input type="text" class="form-control" name="q" placeholder="输入关键字">
															</div>
															<div class="col-sm-1">
																<a class="btn btn-primary search">查询</a>
															</div>
														</div>
													</form>
												</div>
											</div>
										</div>
									</div>
								</div>
							</div>
							<!-- /FORMS QUERY -->
							<div class="row" style="height: 30px;">
								<div class="col-md-12">
									<div class="box batchBtn" style="padding-left: 20px;">
										<input type="radio" name="discountType" value="true"/>&nbsp;按销售价折扣
										<input type="text" class="text" name="discount" placeholder="请输入折扣">
										<input type="radio" name="discountType" value="false"/>&nbsp;按成本价加价
										<input type="text" class="text" class="text"  name="rate" placeholder="请输入比例">
										<a class="btn btn-warning">
											<i class="fa fa-plus-square-o"></i> 保存
										</a>
										<a class="btn btn-success btn-import" href="javascript:;">
											<i class="fa fa-plus-square-o"></i> 批量维护
										</a>
										<a class="btn btn-primary btn-export" href="javascript:;">
											<i class="fa fa-plus-square-o"></i> 导出供货价
										</a>
									</div>
								</div>
							</div>

							<div class="row">
								<!-- TABLES -->
								<div class="col-md-12">
									<!-- BOX -->
									<div class="box border primary">
										<div class="box-title">
										</div>
										<div class="box-body">
											<table id="db_group_list" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">
												
											</table>
										</div>
									</div>
									<!-- /BOX -->
								</div>
							</div>
							<!-- /TABLES -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
			<input type="hidden" name="groupId" />
		</section>

		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->
		<!-- JQUERY -->
		<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
		<!-- JQUERY UI-->
		<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
		<!-- BOOTSTRAP -->
		<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

		<!-- BLOCK UI -->
		<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
		
		<!-- AUTOSIZE -->
		<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
	
		<!-- INPUT MASK -->
		<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>

		<!-- SELECT2 -->
		<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
		<!-- UNIFORM -->
		<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>

		
		<!-- spinner -->
		<script src="../../js/spinner/spin.js"></script>
		<!-- DATA TABLES -->
		<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
		<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
		<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>

		<!--bootbox-->
		<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
		<!-- COOKIE -->
		<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
		<!-- CUSTOM SCRIPT -->
		<script src="../../js/script.js"></script>
		<script src="../../js/common.js"></script>
		<script>

	var Group=function(){			
		var $mainDataTable=null;
		
        var searchGroup = function(){
			var url=App.getContextPath()+"admin/store/searchSkuInfoList.do?q="+$("input[name=q]").val()
					+"&productCode="+$("input[name=productCode]").val()+"&supplierId="+supplierId
					+"&hasSkuSupplierPrice=true";
			var serialNum = 0;
			var dtSetting = $.dataTableSetting({					
				"ajaxSource" :url,
				"serverSide" : true,
				"bFilter" : false,// 去掉搜索框
				"bLengthChange" : true,// 每页显示数量	
					
				"sScrollX":"100%",
				"sScrollXInner": "100%",
	            "bSort": false,
	            "info": false,
				"iDisplayLength": 25,
				"enableCheckbox": false,
				"bSort" : false,					
				"aoColumns" : [ {
					"mDataProp" : "productCode",
					"sTitle" : "商品编码",
					"width" : "15%"
				}, {
					"mDataProp" : "productName",
					"sTitle" : "商品名称",
					"width" : "20%"		
				}, {
					"mDataProp" : "skuCode",
					"sTitle" : "SKU编码",
					"width" : "15%",
					"sClass" : "skuCode"
				}, {
					"mDataProp" : "sizeName",
					"sTitle" : "尺码",
					"width" : "10%"	
				}, {
					"mDataProp" : "salesPrice",
					"sTitle" : "销售价",
					"width" : "10%",
					"sClass" : "salesPrice",
					"render" : function(data, type, row){
						return data.toFixed(2);
					}
				}, {
					"mDataProp" : "costPrice",
					"sTitle" : "成本价",
					"width" : "10%",
					"sClass" : "costPrice",
					"render" : function(data, type, row){
						return data.toFixed(2);
					}	
				}, {
					"mDataProp" : "supplyPrice",
					"sTitle" : "供货价",
					"width" : "10%",
					"sClass" : "red",
					"render" : function(data, type, row){
						return '<font color="red">' + (data == null || data == '' ? '0.00' : data.toFixed(2)) + '</font>';
					}
				}, {						
					"sTitle" : "操作",					
					"render":function(data, type, row){
						var str="<p class='btn-toolbar' style='margin:0px'>"+
					     			"<a class='btn btn-success btn-xs bt_edit btn-width' href='javascript:;' skuId='" + row.skuId + "' skuSupplyPriceId='"+row.skuSupplyPriceId+"'>"+
					     			"<i class='fa fa-arrow-circle-o-up'></i>编辑</a>"+
					     		"</p>";
						 return str;
					}
				}]   
			});
			
			$mainDataTable = $("#db_group_list").dataTable(dtSetting);
		}
        
        var updateSkuSupplyPrice = function() {
        
       	 	$.confirm("你确认保存供货价吗?",function(){
       	 		var skuSupplyPrice = {};
   			 	skuSupplyPrice.skuId = $("#box-config input[name='skuId']").val();
			 	skuSupplyPrice.supplierId = supplierId;
			 	var skuSupplyPriceId = $("#box-config input[name='skuSupplyPriceId']").val();
			 	skuSupplyPrice.id = (skuSupplyPriceId == '' || skuSupplyPriceId == 'null' ? null : skuSupplyPriceId);
			 	skuSupplyPrice.supplyPrice = $("#box-config input[name='supplyPrice']").val();
			 	
	       		$.ajax({
					type: "post",
					url: App.getContextPath()+"admin/store/updateSkuSupplyPrice.do",
					data: {supplierId:supplierId, skuSupplyPrice:JSON.stringify(skuSupplyPrice)},
					async: true,			
					success: function(result) {
						if (result.code = '0000') {
							var item = result.extra;
							$("#db_group_list tbody tr").each(function(){
								if($(this).find(".bt_edit").attr('skuId').trim()==item.skuId){
									$(this).find(".red").html('<font color="red">' + item.supplyPrice + '</font>');
									$(this).find(".bt_edit").attr('skuSupplyPriceId', item.id)
									return false;
								}
							});
							$("#box-config").modal("hide");
						} else {
			        		 $.alert('请稍后重试');
						}
					}
				});
       	 	});
        }
        
        var batchUpdateSkuSupplyPrice = function() {
        	 var discountType = $(".batchBtn input[name='discountType']:checked").val();
        	 if (discountType == null || discountType == '') {
        		 $.alert('请选择供货价计算类型');
        		 return false;
        	 }
			 var discount;
        	 if (discountType == 'true') {
        		 discount = $(".batchBtn input[name='discount']").val();
        	 } else {
        		 discount = $(".batchBtn input[name='rate']").val();
        	 }
        	 if (discount == null || discount == '') {
        		 $.alert('请输入折扣或比例');
        		 return false;
        	 }
        	 $.confirm("你确认保存当前所有商品供货价吗?",function(){
        		 $.ajax({
					type: "post",
					url: App.getContextPath()+"admin/store/updateAllSkuSupplyPrice.do",
					data: {supplierId:supplierId, discountType:discountType, discount:discount, q:$("input[name=q]").val(), productCode:$("input[name=productCode]").val()},
					async: true,			
					success: function(result) {
						if (result.code = '0000') {
							searchGroup();
						} else {
			        		 $.alert('请稍后重试');
						}
					}
				 });
        	 });
        }
	 
		return {
			init:function(){
				supplierId = $.getUrlParam("sid");
			    searchGroup();
			    $(".search").click(searchGroup);
			    $(".batchBtn .btn-warning").click(batchUpdateSkuSupplyPrice);
			    $(".batchBtn .btn-success").click(function(){
			    	
			    });
			    $("#db_group_list").on("click",".bt_edit",function() {
				    $("#box-config input[name='editdiscountType'][value='true']").click();
		        	$("#box-config input[name='discount']").val('');
		        	var $tr = $(this).parents("tr");
		        	$("#box-config input[name='skuCode']").val($tr.find(".skuCode").text());
		        	$("#box-config input[name='salesPrice']").val($tr.find(".salesPrice").text());
		        	$("#box-config input[name='costPrice']").val($tr.find(".costPrice").text());
		        	$("#box-config input[name='supplyPrice']").val($tr.find(".red").text());
		        	$("#box-config input[name='skuId']").val($(this).attr('skuId'));
		        	$("#box-config input[name='skuSupplyPriceId']").val($(this).attr('skuSupplyPriceId'));
		        	$("#box-config").modal("show");
			    });
			    $("#box-config .bt-save").click(updateSkuSupplyPrice);
			    $(".batchBtn input[name='discountType']").click(function(){
			    	if ($(this).val() == 'true') {
			    		$(".batchBtn input[name='rate']").val('');
			    		$(".batchBtn input[name='rate']").attr('disabled', 'disabled');
			    		$(".batchBtn input[name='discount']").removeAttr('disabled');
			    	} else {
			    		$(".batchBtn input[name='discount']").val('');
			    		$(".batchBtn input[name='discount']").attr('disabled', 'disabled');
			    		$(".batchBtn input[name='rate']").removeAttr('disabled');
			    	}
			    });
			    $(".batchBtn input[name='discountType'][value='true']").click();
	        	$(".batchBtn input[name='discount']").val('');
			    $("#box-config input[name='editdiscountType']").click(function(){
			    	if ($(this).val() == 'true') {
			    		$("#box-config input[name='rate']").val('');
			    		$("#box-config input[name='rate']").attr('disabled', 'disabled');
			    		$("#box-config input[name='discount']").removeAttr('disabled');
			    	} else {
			    		$("#box-config input[name='discount']").val('');
			    		$("#box-config input[name='discount']").attr('disabled', 'disabled');
			    		$("#box-config input[name='rate']").removeAttr('disabled');
			    	}
			    });
			    $(".batchBtn .btn-import").click(function(){
			    	window.location.href="supply_price_import.html?sid="+supplierId;
			    });
			    $(".batchBtn .btn-export").click(function(){
			    	location.href = App.getContextPath() + "admin/store/exportSupplier.do?q="+$("input[name=q]").val()+"&productCode="+$("input[name=productCode]").val()+"&supplierId="+supplierId;
			    });
			}
	 
		}
	}();
	 
	$(function(){
		$("#header").load("../head.html");
		App.init();
		Group.init();
	});
		
		</script>
		<!-- /JAVASCRIPTS -->
	</body>

</html>